package com.yutel.socket.nio;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Client {

	public static void main(String[] args) {
		Socket client = null;
		DataInputStream in = null;
		DataOutputStream out = null;
		try {
			client = new Socket("127.0.0.1", 8888);
			client.setSoTimeout(40000);
			//
			out = new DataOutputStream(client.getOutputStream());
			byte[] sendmsg = "asdf".getBytes();
//			out.writeInt(sendmsg.length);
			out.write(sendmsg);
			out.flush();
			//
			in = new DataInputStream(client.getInputStream());
//			int msgLen = in.readInt();
//			System.out.println("clien size=" + msgLen);
			byte[] data = new byte[1024];
			in.readFully(data);
			System.out.println("service msg=" + byte2Str(data));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static String byte2Str(byte[] bs) {
		StringBuffer sb=new StringBuffer();
		for(byte b:bs){
			sb.append((char)b);
		}
		return sb.toString();
	}
}
